#include <stdio.h>

unsigned short GET_RS485_MODBUS_RTU_CRC(unsigned char *data, unsigned short length) 
{
    unsigned short j;
    unsigned short reg_crc = 0xffff;
    while (length--) {
        reg_crc ^= *data++;
        for (j = 0; j < 8; j++) {
            if (reg_crc & 0x01) {
                reg_crc = (reg_crc >> 1) ^ 0xa001;
            } else {
                reg_crc = reg_crc >> 1;
            }
        }
    }
    return reg_crc;
}

int main()
{
    unsigned char data[] = { 0x01, 0x03, 0x13, 0x08, 0x00, 0x02, 0x41, 0x4D };
    int d_size = sizeof(data);
    unsigned short res = GET_RS485_MODBUS_RTU_CRC(data, d_size - 2);
    printf("res = %x\n", res);
    if (res == (data[d_size - 1] << 8 | data[d_size - 2]))
        printf("original res = %x\n", data[d_size - 1] << 8 | data[d_size - 2]);
    
    int motor_position = data[3] << 24 | data[4] << 16 | data[5] << 8 | data[6];
    printf("motor_position = %d\n", motor_position);

    return 0;
}